home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6553 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.0 KB

  1. Path: news1.cris.com!news
  2. From: aubrey@concentric.net (Aubrey Harrison)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C beginner needs your help ASAP
  5. Date: 19 Feb 1996 12:14:34 GMT
  6. Organization: Concentric Internet Services
  7. Message-ID: <4g9pja$5ss@spectator.cris.com>
  8. References: <4g862f$p0b@risky.ecs.umass.edu> <4g8ahd$p8b@spectator.cris.com> <danpop.824689489@rscernix>
  9. NNTP-Posting-Host: cnc022039.concentric.net
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=US-ASCII
  12. X-Newsreader: WinVN 0.99.6
  13.  
  14. You are correct, I did realize these errors after I made the post. But 1. When 
  15. I  declared buf I had intended to use 4 digits but in the loop decided counting 
  16. to 10 was sufficient to show how sprintf worked, 2. Again, when I declared 
  17. filename I was going to use "data" plus four digits which is eight plus the 
  18. terminating null, 3. I stuck in the ".ext" as an after thought to show that it 
  19. could be done (if needed) and intended to comment it out and did in fact 
  20. overlook that filename was not big enough to hold it with the extra chars. Of 
  21. course it was a small program that was only used to show that sprintf was the 
  22. command he was looking for. 
  23.  
  24. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  25. Aubrey Harrison    aubrey@concentric.net    75320.1606@compuserve.com
  26. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  27. What makes us so bitter against people who outwit us is that
  28. they think themselves cleverer than we are.
  29.                -Francois, Duc de La Rochefoucauld (1613-1680)
  30. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  31.  
  32.  
  33. In article <danpop.824689489@rscernix>, danpop@mail.cern.ch says...
  34. >
  35. >In <4g8ahd$p8b@spectator.cris.com> aubrey@concentric.net (Aubrey Harrison) 
  36. writes:
  37. >
  38. >>This is how I would do it. I tested it and it seems to do what you want. Of 
  39. >                                            ^^^^^^^^
  40. >"It seems" is the keyword here, indeed.
  41. >
  42. >>course, I am no expert, in fact I was called "ignorant and idiot" by Mister 
  43. Dan 
  44. >
  45. >Not only you're no expert, you have also difficulties counting up to ten.
  46. >
  47. >>#include <stdio.h>
  48. >>
  49. >>main()
  50. >>{
  51. >>        char buf[3],filename[9];
  52. >>        int  i;
  53. >>
  54. >>        for(i=0; i<10; i++)
  55. >>        {
  56. >>                sprintf(buf,"%d",i);
  57. >>                strcpy( filename,"data");
  58. >>                strcat( filename, buf );
  59. >>                strcat( filename, ".ext");
  60. >
  61. >strcat and strcpy are _not_ functions returning an int, hence a proper
  62. >declaration is mandatory (even if the value returned is discarded).
  63. >ALWAYS include <string.h> before using string manipulation functions.
  64. >
  65. >Anyway, these 4 lines can be replaced by a single sprintf call:
  66. >
  67. >                sprintf(buf, "data%d.ext", i);
  68. >
  69. >>                printf( "%s\n", filename);
  70. >>        }
  71. >Returning a value from a function (implicitly) defined as returning int
  72. >is always a good idea.
  73. >>}
  74. >
  75. >Let's look at how buf and filename are dimensioned.  
  76. >
  77. >buf will always hold one digit and the terminating null character.  It's
  78. >oversized by one character, but this not a major problem.  Better safe
  79. >than sorry :-)
  80. >
  81. >OTOH, we're in deep trouble with filename, which will have to contain
  82. >strings of the form "dataX.ext", but sizeof "dataX.ext" is 10, not 9.
  83. >Now we're invoking undefined behaviour and we should be sorry for this :-)
  84. >
  85. >Too many mistakes in such a short and simple program, don't you think?
  86. >Badly written code is of no help to anybody.  First learn C _yourself_,
  87. >then try to help others.
  88. >
  89. >Dan
  90. >--
  91. >Dan Pop
  92. >CERN, CN Division
  93. >Email: danpop@mail.cern.ch 
  94. >Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  95.  
  96. -- 
  97. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  98. Aubrey Harrison    aubrey@concentric.net    75320.1606@compuserve.com
  99. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  100. What makes us so bitter against people who outwit us is that
  101. they think themselves cleverer than we are.
  102.                -Francois, Duc de La Rochefoucauld (1613-1680)
  103. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  104.  
  105.